<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
use FileSystem;
use Frame;
use Part;

class ToolsController //这个是一些公共的模块
{
    protected $imgExt = ['jpeg','jpg','png','gif','tiff','raw','bmp'];
    protected $fileExt = ['doc','docx','pdf','xls','xlsx','ppt','txt','rar','zip'];
    protected $return_array = array();
    protected $mac_addr;

    function page_helper($page,$render_limit,$page_limit)
    {
        if (empty($page)){

            $limit = $render_limit;

            $skip = 0;

        }else{

            $limit = $page_limit;

            $skip = $page_limit*($page-1);

        }

        return ['skip'=>$skip,'limit'=>$limit];
    }

    function check_permission($obj)
    {
        $member[] = Auth::id();

        $member = array_merge($member,Part::getPartUser($member));

        //资源共享状态 || 管理员 || 归属者 || 归属者兼职
        if(Auth::user()->hasRole('admin') || in_array($obj->assign,$member)){

            $obj->permission = true;

        }else{

            $obj->permission = false;

        }

        return $obj;
    }

    function file_upload(Request $request)
    {
        if(in_array(strtolower($request->file('file')->getClientOriginalExtension()),$this->imgExt)){

            $file = FileSystem::uploadPublicFile($request->file('file'),'image');

        }elseif (in_array(strtolower($request->file('file')->getClientOriginalExtension()),$this->fileExt)){

            $file = FileSystem::uploadDataFile($request->file('file'),'file');

        }else{

            $file = ['code'=>0,'msg'=>'上传文件不合法'];

        }

        return json_encode($file);

    }

    function GetMacAddr($os_type){
        switch ( strtolower($os_type) ){
            case "linux":
                $this->forLinux();
                break;
            case "solaris":
                break;
            case "unix":
                break;
            case "aix":
                break;
            default:
                $this->forWindows();
                break;
        }

        $temp_array = array();
        foreach ( $this->return_array as $value ){

            if (
            preg_match("/[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f]/i",$value,
                $temp_array ) ){
                $this->mac_addr = $temp_array[0];
                break;
            }

        }
        unset($temp_array);
        return $this->mac_addr;
    }

    function forWindows(){
        @exec("ipconfig /all", $this->return_array);
        if ( $this->return_array )
            return $this->return_array;
        else{
            $ipconfig = $_SERVER["WINDIR"]."\system32\ipconfig.exe";
            if ( is_file($ipconfig) )
                @exec($ipconfig." /all", $this->return_array);
            else
                @exec($_SERVER["WINDIR"]."\system\ipconfig.exe /all", $this->return_array);
            return $this->return_array;
        }
    }

    function forLinux(){
        @exec("ifconfig -a", $this->return_array);
        return $this->return_array;
    }

    function getBetween($flag=true,$format='Y-m-d H:i:s'){
        $startOfMonth = '';
        $endOfMonth = '';

        if ($flag){

            $startOfMonth = Carbon::now()->startOfMonth();

            $endOfMonth = Carbon::now()->endOfMonth();

        }

        if(request()->get('date1') && request()->get('date2')){

            if (request()->get('date1') && $this->checkDate(request()->get('date1'),$format)){

                $startOfMonth = Carbon::parse(request()->get('date1'));

            }
            if(request()->get('date2') && $this->checkDate(request()->get('date2'),$format)){

                $endOfMonth = Carbon::parse(request()->get('date2'))->endOfDay();

            }

        }

        if ($startOfMonth && $endOfMonth){

            $between = [$startOfMonth,$endOfMonth];

        }else{

            $between = [];

        }

        return $between;

    }

    function checkDate($date,$format='Y-m-d H:i:s'){

        if (date($format,strtotime($date))==$date){

            return true;

        }else{

            return false;

        }

    }

    function getMonthBetween(){

        $startOfMonth = Carbon::now()->startOfMonth();

        $endOfMonth = Carbon::now()->endOfMonth();

        if(request()->get('date1') && request()->get('date2')){

            if (request()->get('date1') && $this->checkMonthDate(request()->get('date1'))){

                $startOfMonth = Carbon::parse(request()->get('date1'));

            }
            if(request()->get('date2') && $this->checkMonthDate(request()->get('date2'))){

                $endOfMonth = Carbon::parse(request()->get('date2'));

            }

        }

        $between = [$startOfMonth,$endOfMonth];

        return $between;

    }

    function checkMonthDate($date){

        if (date('Y-m',strtotime($date))==$date){

            return true;

        }else{

            return false;

        }

    }

    public function checkDown($obj){

        $obj->display = 0;

        $member = [Auth::id()];

        $member = array_merge($member,Part::getPartUser($member));

        //管理员 || 归属者 || 归属者领导
        if(Auth::user()->hasRole('admin') || Auth::id()==$obj->uid || in_array($obj->assign,$member)){

            $obj->display = 1;

        }else{

            $obj->load(['download_log'=>function($query){
                $query->where('uid',Auth::id());
            }]);

            $obj->display = count($obj->download_log);

            unset($obj->download_log);

        }

        return $obj;

    }

}